package com.yuexun.dhdoctor.module.contractmanage.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Filter;
import android.widget.Filterable;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.contractmanage.bean.ConTractManageBean;
import com.yuexun.dhdoctor.module.contractmanage.ui.Act_ContractManageDetail;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by work-jx on 2016/4/8.
 */
public class ContractManageAdapter extends KJAdapter<ConTractManageBean.RowsBean> implements Filterable {
    private ArrayList<ConTractManageBean.RowsBean> contractManageListBeans = new ArrayList<>();
    ArrayList<ConTractManageBean.RowsBean> contractManageListBeans_filter = new ArrayList<>();
    private PatientFilter filter;

    public ContractManageAdapter(AbsListView view, Collection<ConTractManageBean.RowsBean> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        contractManageListBeans = (ArrayList<ConTractManageBean.RowsBean>) ((ArrayList)mDatas).clone();
    }

    @Override
    public void convert(AdapterHolder adapterHolder, ConTractManageBean.RowsBean contractManageList, boolean b) {
        adapterHolder.setText(R.id.contractmanage_name,contractManageList.getName());
        adapterHolder.setText(R.id.contractmanage_phone,contractManageList.getLinkphone());
        adapterHolder.setText(R.id.contractmanage_address, contractManageList.getAddress());
        if ("1".equals(contractManageList.getSex())) {
            adapterHolder.setImageResource(R.id.contractmanage_item_icon,R.drawable.pic_man);
        } else if ("2".equals(contractManageList.getSex())) {
            adapterHolder.setImageResource(R.id.contractmanage_item_icon,R.drawable.pic_woman);
        } else {
            adapterHolder.setImageResource(R.id.contractmanage_item_icon,R.drawable.pic_man);
        }
        onPicClick(adapterHolder.getConvertView(),contractManageList);
    }

    private void onPicClick(View view,final ConTractManageBean.RowsBean contractManageList) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("contractManageDetail",contractManageList);
                Intent intent = new Intent(mCxt, Act_ContractManageDetail.class);
                intent.putExtras(bundle);
                mCxt.startActivity(intent);
            }
        });
    }

    @Override
    public Filter getFilter() {
        if(filter == null){
            filter = new PatientFilter();
        }
        return filter;
    }

    class PatientFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            if(constraint == null || constraint.length() == 0){
                results.values = contractManageListBeans;
                results.count = contractManageListBeans.size();
            }else{
                contractManageListBeans_filter.clear();
                for(ConTractManageBean.RowsBean p : contractManageListBeans){
                    if(p.getName().contains(constraint.toString())){
                        contractManageListBeans_filter.add(p);
                    }
                }
                results.values = contractManageListBeans_filter;
                results.count = contractManageListBeans_filter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            refresh((Collection<ConTractManageBean.RowsBean>) results.values);
        }
    }

}
